package cn.iocoder.yudao.framework.magic.config.config;

import cn.iocoder.yudao.framework.datasource.core.enums.DataSourceEnum;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;

public class MagicApiDynamicConfig {


    @Resource
    private DynamicDataSourceProvider dynamicDataSourceProvider;
    @Resource
    private DynamicDataSourceProperties dynamicDataSourceProperties;
    @Bean
    public MagicDynamicDataSource magicDynamicDataSource(){
        Map<String, DataSource> stringDataSourceMap = dynamicDataSourceProvider.loadDataSources();
        MagicDynamicDataSource dynamicDataSource = new MagicDynamicDataSource();

        dynamicDataSource.setDefault(stringDataSourceMap.get(DataSourceEnum.MASTER)); // 设置默认数据源
        dynamicDataSource.add(DataSourceEnum.MASTER,stringDataSourceMap.get(DataSourceEnum.MASTER));
        dynamicDataSource.add(DataSourceEnum.SLAVE,stringDataSourceMap.get(DataSourceEnum.SLAVE));

        return dynamicDataSource;
    }
}
